package cn.wenhaha.plugin;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.setting.dialect.Props;
import cn.wenhaha.common.bean.PluginInfo;
import cn.wenhaha.common.utils.PluginUtils;
import cn.wenhaha.datasource.IDataSourcePlugin;
import org.junit.Test;
import org.pf4j.DefaultPluginManager;
import org.pf4j.JarPluginManager;
import org.pf4j.PluginManager;
import org.pf4j.PluginWrapper;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Paths;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * --------
 *
 * @author ：wyndem
 * @Date ：Created in 2023-01-24 12:54
 */

public class JarLoader {

    @Test
    public void test1() {
        PluginManager pluginManager = new DefaultPluginManager();
        pluginManager.loadPlugins();
        pluginManager.startPlugins();

        List<IDataSourcePlugin> extensions = pluginManager.getExtensions(IDataSourcePlugin.class);
        System.out.println(extensions);


        PluginWrapper mysqlHikariC = pluginManager.getPlugin("mysqlHikariC");
        System.out.println(mysqlHikariC);
        ClassLoader classLoader = mysqlHikariC.getPluginClassLoader();
        URL resource = classLoader.getResource("main.properties");
        Props props = new Props(resource, CharsetUtil.UTF_8);
        System.out.println(props);

        InputStream resourceAsStream = classLoader.getResourceAsStream("template.zip");
        System.out.println(resourceAsStream);
        IoUtil.close(resourceAsStream);

        boolean mysqlHikariC1 = pluginManager.deletePlugin("mysqlHikariC");
        System.out.println(mysqlHikariC1);


    }


    @Test
    public void test2() {
        JarPluginManager pluginManager = new JarPluginManager();

        String path = "C:\\Users\\Admin\\Desktop\\sync\\config\\datalib\\Mysql数据池_v0.3_Luo Gen_1674739473644.jar";
        String id = pluginManager.loadPlugin(Paths.get(path));
        pluginManager.startPlugin(id);

        assertTrue(pluginManager.deletePlugin(id));
        assertFalse(FileUtil.exist(path));


//
//        PluginInfo info = pluginManager.info(path);
//        System.out.println(info.getId());

    }

    @Test
    public void test3() {
        PluginUtils pluginManager = PluginUtils.getPluginManager();
        String path = "C:\\Users\\Admin\\Desktop\\sync\\config\\datalib\\SF数据支持库_v0.2_Luo Gen_1674739471325 - 副本 (3).jar";
        String path1 = "C:\\Users\\Admin\\Desktop\\sync\\config\\datalib\\SF数据支持库_v0.2_Luo Gen_1674739471325 - 副本 (4).jar";

        PluginInfo info = pluginManager.info(path);
        String s = pluginManager.loadAndStart(path);
        pluginManager.getObject(IDataSourcePlugin.class, s).getEventListen().unLoad();
        FileUtil.move(new File(path), new File(path1), true);
        assertTrue(pluginManager.delete(s));
        assertFalse(FileUtil.exist(path));
    }


}
